<template>
    <div>
        <div v-show="limit > imgList.length">
            <img src="../../static/images/uploadImg2.png" style="height: 4rem;margin-left: 8px;"
                 :style="{width:limit === 1 ?'auto':'3rem'}" @click="uploadPic"/>
        </div>
    </div>
</template>

<script>
    export default {
        name: "cameras-and-albums",
        props: {
            limit: {
                type: Number,
                default: 1,
            },
        },
        data() {
            return {
                datas: new FormData(),
                files: 0,
                size: 0
            }
        },
        mounted() {
            //将要给原生调用的方法挂载到 window 上面
            window.handlegetupload = this.handlegetupload;
        },

        computed:{
            imgList(){
                return  this.$store.state.user.imgList
            },
        },

        methods: {
            uploadPic() {
                let param = {type: 1, callback: 'handlegetupload'}
                let paramJSON = JSON.stringify(param);
                try {
                    if (this.isApple()) {
                        webkit.messageHandlers[method].postMessage(paramJSON);
                    } else {
                        UFFClient.upload(paramJSON);
                    }
                } catch (e) {
                    if (param && param.callback) {
                        eval(`${param.callback}(null);`);
                    }
                }

            },

            handlegetupload(data) {
                let _imgList = this.imgList;
                if (data.url) {
                    _imgList.push({fileurl: data.url})
                } else {
                    this.$emit('pictureUrl', '')
                    this.$emit('on-change')
                    return;
                }
                if (_imgList.length) {
                    let _img = '';
                    _imgList.forEach(item => {
                        _img = _img ? item.fileurl : _img + ',' + item.fileurl;
                    })
                    this.$emit('pictureUrlList', _imgList)
                    this.$store.dispatch('changeImgList', _imgList)
                    this.$emit('pictureUrl', data.url)
                } else {
                    this.$store.dispatch('changeImgList', [])
                    this.$emit('pictureUrl', '')
                    this.$emit('on-change')
                }

            },

            //是否ios系统
            isApple() {
                let userAgent = navigator.userAgent.toLowerCase();
                if (userAgent.indexOf("ios") != -1 || userAgent.indexOf("iphone") != -1 || userAgent.indexOf("ipad") != -1) {
                    return true;
                }
                return false;
            }

        },

    }
</script>

<style>

</style>
